#include "main.h"

void login(int newfd,MSG* pmsg)
{
	/*打印登录用户信息*/
	printf("username:%s\n",pmsg->username);
	printf("password:%s\n",pmsg->info);

	/*验证登录用户信息是否正确*/
	snprintf(sql,size,"select * from users \
		where username = '%s' and password = '%s';",pmsg->username,pmsg->info);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	/*返回验证结果*/
	if(row > 0)
	{
		strcpy(pmsg->info,"SUCCESS");
		write(newfd,pmsg,sizeof(MSG));
		printf("Client login success.\n");
	}
	else
	{
		strcpy(pmsg->info,"FAIL");
		write(newfd,pmsg,sizeof(MSG));
		printf("Client login failed.\n");
	}

	/*等待客户端接收验证结果*/
	sleep(1);
}

void regis(int newfd,MSG* pmsg)
{
	char* logintime;

	/*验证用户是否已注册*/
	snprintf(sql,size,"select * from users \
		where username = '%s';",pmsg->username);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	/*返回验证结果*/
	if(row > 0)
	{
		printf("Client register failed.\n");
		printf("User already exists.\n");
		strcpy(pmsg->info,"FAIL");
		write(newfd,pmsg,sizeof(MSG));
		return;
	}

	/*添加用户*/
	gettime(&logintime);
	snprintf(sql,size,"insert into users values('%s','%s','%s');",pmsg->username,pmsg->info,logintime);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);

	printf("Client register success.\n");
	strcpy(pmsg->info,"SUCCESS");
	write(newfd,pmsg,sizeof(MSG));
}


